//退出登录
function logout() {
    $.ajax({
        url: '/csfy/dologout.do',
        method: 'POST',
        success: function (res) {
            if (JSON.parse(res).code == 200) {
                window.location.href = "/csfy/index.do"
            }
        }
    })
}

//获取当前登录用户信息
$(document).ready(function () {
    $.ajax({
        method: 'POST',
        url: '/csfy/intf.do',
        data: { method: 'getLoginUserInfo' },
        success: res => {
        if (res.code == 10086) {
//                        window.location.href = "/csfy/index.do"
        alert("请重新登录！");
    } else {
        if (res.code == 200) {
            $(".username").html(res.rows.userName);
        }
    }
}
})
})
function getJsonFromUrl() {
    var query = location.search.substr(1);
    var result = {};
    query.split("&").forEach(function (part) {
        var item = part.split("=");
        result[item[0]] = decodeURIComponent(item[1]);
    });
    return result;
}

var id = getJsonFromUrl().id;

if (!id) {

    window.location.href = "/csfy/static/manage-sys/power-manage.html"
} else {
    //用户类型
    function getUserType(type) {
        type = parseInt(type);
        if (isNaN(type)) {
            return ' ';
        }
        if (type == 0) {
            return 'UAMS管理员';
        } else if (type == 1) {
            return '华大员工';
        }else if(type == 2){
            return '外包账号';
        }else if(type == 3){
            return '外部用户';
        }
    }

    //员工状态
    function getUserStatus(type) {
        type = parseInt(type);
        if (isNaN(type)) {
            return '';
        }
        if (type == 1) {
            return '在职';
        } else if (type == 0) {
            return '离职';
        }
    }

    //用户性别
    function getSex(type){
        type = parseInt(type);
        if (isNaN(type)) {
            return '';
        }
        if(type == 0){
            return "未知";
        }
        if (type == 1) {
            return '女';
        } else if (type == 2) {
            return '男';
        }
    }

    //TODO 获取详情信息
    $.ajax({
        url: '/csfy/user/findAllUamsUser.do?userId='+id,
        method: 'POST',
        //data: { method: 'findAllUamsUser.do', userId: id },
        success: function (res) {
            res = JSON.parse(res);
            if (res.code == 200 && res.rows) {
                $("#user-name").html(res.rows[0].userName);
                $("#user-mobile").html(res.rows[0].userMobile);
                $("#real-name").html(res.rows[0].realName);
                $("#user-sex").html(getSex(res.rows[0].userSex));
                $("#user-address").html(res.rows[0].userAddr);
                $("#user-email").html(res.rows[0].userEmail);
                $("#qq-num").html(res.rows[0].userQQ);
                $("#user-type").html(getUserType(res.rows[0].userType));
                $("#user-status").html(getUserStatus(res.rows[0].userStatus));

                //获取权限信息
                var firstMenu = res.rows[0].menuCodeFirst;
                var secondMenu = res.rows[0].menuCodeSecond;

                var fMenuLsit=[];
                var sMenuList=[];
                if(firstMenu != null && firstMenu !="" ){
                    //firstMenu =firstMenu.substr(0,firstMenu.length - 1);
                    fMenuLsit = firstMenu.split(",");
                }

                if(secondMenu !=null && secondMenu !=""){
                    //secondMenu =secondMenu.substr(0,secondMenu.length - 1);
                    sMenuList = secondMenu.split(",");
                }

                for(var i in fMenuLsit) {
                    $("#fMenu-" + fMenuLsit[i]).attr("checked", true);
                }
                if (sMenuList != "" && sMenuList != null) {
                    for (var j in sMenuList) {
                        $("#sub-" + sMenuList[j]).attr("checked", true);
                    }
                }

            }
            /*else {
                alert('获取用户权限信息失败，请稍后重试');
                //window.location.href = "/csfy/static/manage-sys/power-manage.html"
            }*/
        },
        error: function (msg) {
            alert('获取用户权限信息失败，请稍后重试');
            callParent("权限分配","/csfy/static/manage-sys/power-manage.html");
            //window.location.href = "/csfy/static/manage-sys/power-manage.html"
        }
    })
}

function save(){
    var powerList=[];
    $("#power-list .icon-wrapper input[type='checkbox']:checked").each(function (){
        powerList.push($(this).val());
    });
    //console.log(powerList);

    //TODO 提交保存权限信息
    $.ajax({
        url: '/csfy/user/updateUserMenuByUserId.do' ,
        type: 'POST',
        dataType :'json',
        data: { userId: id ,menus:powerList},
        success: function (res) {
            //res = JSON.parse(res);
            //console.log(res);
            if (res.code == 200 && res.rows) {
                alert("提交用户权限信息成功！");
                //window.location.href = "/csfy/static/manage-sys/power-manage.html"
                //跳转到上级tab
                callParent("权限分配","/csfy/static/manage-sys/power-manage.html");
                //关闭当前tab
                closeCurrentTab("分配权限");
            }
        },
        error: function (msg) {
            alert('提交用户权限信息失败，请稍后重试');
            callParent("权限分配","/csfy/static/manage-sys/power-manage.html");
            //关闭当前tab
            closeCurrentTab("分配权限");
            //window.location.href = "/csfy/static/manage-sys/power-manage.html"
        }
    })
}

//跳转到上级tab
function callParent(title,url) {
    // var menuName=parent.$('#tt').tabs('getSelected').panel('options').title;
    //如果存在，直接刷新页面
    if (parent.$('#tt').tabs('exists', title)) {
        parent.$('#tt').tabs('close', title);
        window.parent.addTab(title, url);
    } else {
        window.parent.addTab(title, url);
    }
}

//关闭当前tab
function closeCurrentTab(title) {
    parent.$("#tt").tabs('close',title);
}